package com.mall.db.api;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.UmsRoleMapper;
import com.mall.pojo.UmsRole;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class UmsRoleApiImpl implements UmsRoleApi {

    @Autowired
    private UmsRoleMapper roleMapper;

    /**
     * 根据角色id获取角色信息
     *
     * @param roleId
     * @return
     */
    @Override
    public UmsRole findByRole(Long roleId) {
        LambdaQueryWrapper<UmsRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsRole::getId,roleId);
        return roleMapper.selectOne(wrapper);
    }

    /**
     * 获取所有角色信息
     *
     * @return
     */
    @Override
    public List<UmsRole> findAll() {
        LambdaQueryWrapper<UmsRole> wrapper = new LambdaQueryWrapper<>();
        return roleMapper.selectList(wrapper);
    }

    /**
     * 根据角色名称分页获取角色列表
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @Override
    public Page<UmsRole> list(Integer pageNum, Integer pageSize, String keyword) {
        Page<UmsRole> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<UmsRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(keyword),UmsRole::getName,keyword)
                .orderByDesc(UmsRole::getSort);
        roleMapper.selectPage(page,wrapper);
        return page;
    }

    @Override
    public UmsRole findByName(String name) {
        LambdaQueryWrapper<UmsRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsRole::getName,name);
        return roleMapper.selectOne(wrapper);
    }

    @Override
    public Integer addRole(UmsRole role) {
        return roleMapper.insert(role);
    }

    @Override
    public Integer update(UmsRole role) {
        return roleMapper.updateById(role);
    }

    @Override
    public UmsRole findById(Integer ids) {
        return roleMapper.selectById(ids);
    }

    @Override
    public Integer delete(Integer ids) {
        return roleMapper.deleteById(ids);
    }
}
